/* 算法实现模板 */
import AlgorithmBase from "./AlgorithmBase";
import TDom from "../../../common/TDom";
import React, { useState } from 'react';
import { AgGridReact } from 'ag-grid-react';
import ReactDom, { unmountComponentAtNode } from 'react-dom';
import {App} from "../../../visual/AgGrid";
import 'ag-grid-community/dist/styles/ag-grid.css';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';

class ObservationSeqGenAlg extends AlgorithmBase{
  constructor(){
    super('ObservationSeqGenAlg');
    this.timeStep = 3
    this.experimentStep = 1
  }
  

  initModelUI(){
    var stepLabel = TDom.createLabel(this.domElementList.model,'时序数(x)：','',document.getElementById('timeStep'))
    var stepButton = TDom.createNumberInput(this.domElementList.model,()=>{this.timeStep = document.getElementById('timeStep').value;},3,1,'timeStep')
    var experimentLabel = TDom.createLabel(this.domElementList.model,'实验次数(y)：','',document.getElementById('experimentStep'))
    var experimentButton = TDom.createNumberInput(this.domElementList.model,()=>{this.experimentStep = document.getElementById('experimentStep').value;},1,1,'experimentStep')
    stepButton.min= 1
    experimentButton.min =1
  }

  initTrainDS (){
    this.pi =[]
    this.A =[]
    this.B =[]
    this.box =[]
    this.ball =[]
    this.pi = [0.25,0.25,0.25,0.25]
    this.A = [[0,1,0,0],[0.4,0,0.6,0],[0,0.4,0.6,0],[0,0,0.5,0.5]]
    this.B = [[0.5,0.5],[0.3,0.7],[0.6,0.4],[0.8,0.2]]
    this.box = ['盒子1','盒子2','盒子3','盒子4']
    this.ball = ['red','white']
    this.distributionPoint(this.pi,this.A,this.B)
  }

  //创建随机数模拟概率
  createRandomNumber(begin,end,precision){
    var res = (Math.random()*(end-begin)).toFixed(precision)
    return res;
  }

  distributionPoint(pi,A,B){
    var nowPoint = 0
    for(let i =0 ;i<pi.length;i++){
      var temp = []
      temp.push(nowPoint)
      temp.push(nowPoint + pi[i])
      pi[i] = temp
      nowPoint = pi[i][1]
    }
    nowPoint = 0 
    for(let i =0 ;i<A.length;i++){
      for(let j =0;j<A[i].length;j++){
        var temp = []
        temp.push(nowPoint)
        temp.push(nowPoint + A[i][j])
        A[i][j] =temp 
        nowPoint = A[i][j][1]  
      }
      temp =[]
      nowPoint = 0
    }
    for(let i =0 ;i<B.length;i++){
      for(let j =0;j<B[i].length;j++){
        var temp = []
        temp.push(nowPoint)
        temp.push(nowPoint + B[i][j])
        B[i][j] =temp 
        nowPoint = B[i][j][1]
      }
      nowPoint = 0
      temp=[]
    }
  }

  //初始box选择 返回选中box索引也就是A的行数
  piStart(){
    var seed = this.createRandomNumber(0,1,2)
    for(let i =0;i<this.pi.length;i++){
      if (seed <=this.pi[i][1]&&seed>=this.pi[i][0])
        {
          return i
        }
    }
  }


  //返回选中盒子后的下一个盒子的索引值也即B的行值
  chooseA(index){
    var seed = this.createRandomNumber(0,1,2)
      for(let i =0;i<this.A[index].length;i++){
        if (seed <=this.A[index][i][1]&&seed>=this.A[index][i][0]){
          return i
        }   
      }
  }

  //选出盒子后按概率取球0为红球1为白球
  chooseB(index){
    var seed = this.createRandomNumber(0,1,2)
    for(let i =0;i<this.B[index].length;i++){
      if (seed <=this.B[index][i][1]&&seed>=this.B[index][i][0]){
        return i
      }   
    }
  }

  train(){
    var resOutput =[]
    var startBox = this.piStart()
    for(let expertime = 0 ;expertime<this.experimentStep;expertime++){
      var datatemp =[]
      for(let i =0;i<this.timeStep;i++){
        if(this.chooseB(startBox)){
          datatemp.push('white')
        }else{
          datatemp.push('red')
        }
        startBox = this.chooseA(startBox)
      }
      resOutput.push(datatemp)
      var temptitle =[]
      for(let i = 0 ;i<this.timeStep;i++){
        temptitle.push(`第${i}次`)
      }
    }
    unmountComponentAtNode(this.domElementList.output)
    ReactDom.render(<App title={temptitle} data={ resOutput}/>,this.domElementList.output)
  }

  trainAnimation(){
    
  }

  openFileSelector(){
    
  }

  initInputUI(){
    const fileSelector      = document.createElement('input');
    // fileSelector.type       = 'file';
    // fileSelector.hidden     = true;
    // fileSelector.onchange   = this.handleFileChange.bind(this);   
    
   
    TDom.createButton(this.domElementList.input, "获取服务器数据", this.initTrainDS.bind(this));
    this.addButton(this.domElementList.input, "使用本地数据", this.openFileSelector.bind(this));
    // this.domElementList.input.appendChild(fileSelector);
    this.addButton(this.domElementList.input, "生成", this.train.bind(this));
    this.playButton = this.addButton(this.domElementList.input, "训练动画", this.trainAnimation.bind(this));
    this.playButton.hidden = true;

    this.labelTrainTips = this.addLabel(this.domElementList.input, '');
    this.inputDiv       = this.addDiv(this.domElementList.input);

    this.fileSelector  = fileSelector;
  }

}


export default ObservationSeqGenAlg;